package com.oig.sys.base.controller;


import com.oig.common.module.SysResponse;
import com.oig.common.validator.GroupAdd;
import com.oig.common.validator.GroupEdit;
import com.oig.sys.base.dto.EnumDto;
import com.oig.sys.base.service.ITbEnumItemService;
import com.oig.sys.base.service.ITbEnumService;
import com.oig.sys.base.vo.EnumItemGroupVo;
import com.oig.sys.base.vo.EnumItemVo;
import com.oig.sys.base.vo.EnumVo;
import com.oig.sys.log.annotation.SysLog;
import com.oig.sys.security.util.SecurityUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;
import java.util.Map;


@Tag(name = "自定义枚举字典管理")
@RestController
@RequestMapping("/enum")
@Slf4j
public class TbEnumController {

    private final ITbEnumService enumService;
    private final ITbEnumItemService enumItemService;

    @Autowired
    public TbEnumController(ITbEnumService enumService, ITbEnumItemService enumItemService){
        this.enumService = enumService;
        this.enumItemService = enumItemService;
    }

    @PreAuthorize("@pms.hasPermission('/enum/addEnum')")
    @SysLog(value="添加字典",apiUrl="/enum/addEnum")
    @Operation(summary = "添加字典,添加字典")
    @PostMapping(value = "/addEnum")
    public EnumVo addEnum(@RequestBody @Validated(GroupAdd.class) EnumDto enumDto){
        log.info("add enum {}", enumDto);
        enumDto.setCreateTime(new Date());
        enumDto.setCreateUserId(SecurityUtil.getDefineUserMain().getId());
        enumDto.setCreateUserName(SecurityUtil.getDefineUserMain().getRealName());
        enumDto.setLastUpdateTime(new Date());
        enumDto.setLastUpdateId(SecurityUtil.getDefineUserMain().getId());
        enumDto.setLastUpdateName(SecurityUtil.getDefineUserMain().getRealName());
        return this.enumService.addTbEnum(enumDto);
    }

    @PreAuthorize("@pms.hasPermission('/enum/updateEnum')")
    @SysLog(value="更新字典",apiUrl="/enum/updateEnum")
    @Operation(summary = "更新字典")
    @PostMapping(value = "/updateEnum")
    public EnumVo updateEnum(@RequestBody @Validated(GroupEdit.class) EnumDto enumDto){
        log.info("update enum {}", enumDto);
        enumDto.setLastUpdateTime(new Date());
        enumDto.setLastUpdateId(SecurityUtil.getDefineUserMain().getId());
        enumDto.setLastUpdateName(SecurityUtil.getDefineUserMain().getRealName());
        return this.enumService.updateTbEnum(enumDto);
    }

    @PreAuthorize("@pms.hasPermission('/enum/delEnum')")
    @SysLog(value="删除字典",apiUrl="/enum/delEnum")
    @Operation(summary = "删除字典")
    @Parameter(name = "id",description = "字典id")
    @PostMapping(value = "/delEnum/{id}")
    public SysResponse<Integer> delEnum(@PathVariable("id") Integer id){
        log.info("cancel enum id={}", id) ;
        this.enumService.delEnum(id);
        return SysResponse.ok(id,"删除字典成功");
    }


    @Operation(summary = "查看字典明细")
    @Parameter(name = "id", description = "字典id")
    @PostMapping(value = "/info/{id}")
    public EnumVo info(@PathVariable("id") Integer id){
        log.info("get enum by dict id={}", id) ;
        return this.enumService.getTbEnum(id);
    }

    @Operation(summary = "根据代码获取字典明细")
    @Parameter(name = "dictCode", description = "字典code")
    @PostMapping(value = "/getByCode/{dictCode}")
    public EnumVo getByCode(@PathVariable("dictCode") String dictCode){
        log.info("get enum by dict code={}", dictCode) ;
        return this.enumService.getTbEnum(dictCode);
    }

    @Operation(summary = "查询字典枚举值")
    @Parameters({
            @Parameter(name = "dictId", description = "字典id"),
            @Parameter(name = "searchKey", description = "枚举名称或代码")
    })
    @PostMapping(value = "/searchItems/{dictId}")
    public List<EnumItemVo> searchEnumItem(@PathVariable("dictId") Integer dictId, @RequestParam("searchKey") String searchKey){
        log.info("query enumItems by dictId={} and searchCode={}", dictId, searchKey);
        return this.enumItemService.searchEnumItem(dictId, searchKey);
    }

    @PreAuthorize("@pms.hasPermission('/enum/listEnum')")
    @Operation(summary = "查询字典列表")
    @Parameter(name = "searchKey",description = "字典名称或代码")
    @PostMapping(value = "/listEnum")
    public List<EnumVo> listEnum(@RequestParam("searchKey")String searchKey){
        log.info(" list enum :{}", searchKey);
        return this.enumService.listEnum(searchKey);
    }

    @Operation(summary = "获取所有字典,前端获取后缓存")
    @PostMapping(value = "/getAllEnum")
    public Map<String, Map<String, EnumItemGroupVo>> getAllEnum(){
        log.info("get all enums...");
        return this.enumService.getAllEnum();
    }


    @Operation(summary = "获取所有字典")
    @PostMapping(value = "/getAllEnumItem")
    public Map<String, Map<String, String>> getAllEnumItem(){
        log.info("get all enums and item...");
        return this.enumService.getAllEnumItem();
    }

}
